/*
 *  Copyright 2009 Advanced Industrial Science and Technology (AIST).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file
 *  \brief
 */
#pragma once

// thrust, includes
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/pending/integer_range.hpp>

// cuda, includes
#include <cuda_runtime.h>

#include <iostream>
//#include <typeinfo>

#include <thrust/mpl/bool.hpp>

namespace thrust {

  template <typename Graph>
  struct graph_traits
  {
    typedef typename Graph::vertex_property_type vertex_property_type;
    typedef typename Graph::edge_property_type edge_property_type;

    //typedef typename Graph::space_tag          space_tag;
    typedef typename Graph::directed_category      directed_category;
    typedef typename Graph::edge_parallel_category edge_parallel_category;
    typedef typename Graph::traversal_category     traversal_category;

    typedef typename Graph::vertices_size_type vertices_size_type;
    typedef typename Graph::edges_size_type    edges_size_type;
    typedef typename Graph::degree_size_type   degree_size_type;

    typedef typename Graph::vertex_descriptor  vertex_descriptor;
    typedef typename Graph::vertex_iterator    vertex_iterator;

    typedef typename Graph::edge_descriptor    edge_descriptor;
    typedef typename Graph::out_edge_iterator  out_edge_iterator;
    typedef typename Graph::in_edge_iterator   in_edge_iterator;
    
    typedef typename Graph::kernel_out_edge_iterator kernel_out_edge_iterator;
    typedef typename Graph::kernel_in_edge_iterator  kernel_in_edge_iterator; 

    typedef typename Graph::adjacency_iterator        adjacency_iterator;
    typedef typename Graph::kernel_adjacency_iterator kernel_adjacency_iterator;

    typedef typename Graph::edge_iterator        edge_iterator;
    typedef typename Graph::kernel_edge_iterator kernel_edge_iterator;

    static inline vertex_descriptor null_vertex();
  };
  
  
  template <typename Graph>
  inline typename graph_traits<Graph>::vertex_descriptor
  graph_traits<Graph>::null_vertex() {
    return Graph::null_vertex();
  }

  //-----------------------------------------------------------
  // directed_category_tags
  struct called_from_tag {};
  struct called_from_host_tag : called_from_tag {};
  struct called_from_device_tag : called_from_tag {};

  //-----------------------------------------------------------
  // directed_category_tags
  struct directed_tag {};
  struct undirected_tag {};
  struct bidirectional_tag : public directed_tag {};
  
  namespace detail {
    inline bool is_directed( directed_tag) { return true;}
    inline bool is_directed( undirected_tag) { return false;}
  }

  /** Retuen true if the given graph is directed. */
  template <typename Graph>
  bool is_directed( const Graph&) {
    typedef typename graph_traits<Graph>::directed_category Cat;
    return detail::is_directed(Cat());
  }

  /** Return false if the given graph is undirected. */
  template <typename Graph>
  bool is_undirected( const Graph& g) {
    return !is_directed(g);
  }
  //-----------------------------------------------------------


  //-----------------------------------------------------------
  // edge_parallel_category_tags
  struct allow_parallel_edge_tag {};
  struct disallow_parallel_edge_tag {};
  
  namespace detail {
    inline bool allows_parallel( allow_parallel_edge_tag) { return true;}
    inline bool allows_parallel( disallow_parallel_edge_tag) { return false;}
    inline bool disallows_parallel( allow_parallel_edge_tag) { return false;}
    inline bool disallows_parallel( disallow_parallel_edge_tag) { return true;}
  }
  
  template <typename Graph>
  bool allows_parallel_edges( const Graph&) {
    typedef typename graph_traits<Graph>::edge_parallel_category Cat;
    return detail::allows_parallel(Cat());
  }
  
  //-----------------------------------------------------------


  //-----------------------------------------------------------
  // traversal_category_tags
  struct incidence_graph_tag {};
  struct adjacency_graph_tag {};
  struct bidirectional_graph_tag : virtual incidence_graph_tag {};
  struct vertex_list_graph_tag {};
  struct edge_list_graph_tag {};

  //-----------------------------------------------------------


  //-----------------------------------------------------------
  // property types
  template <typename Graph>
  struct edge_property_type {
    typedef typename Graph::edge_property_type type;
  };
  template <typename Graph>
  struct vertex_property_type {
    typedef typename Graph::vertex_property_type type;
  };
  template <typename Graph>
  struct graph_property_type {
    typedef typename Graph::graph_property_type type;
  };
  
} //end namespace thrust
